//
//  YSHomeViewController.swift
//  新浪浪
//
//  Created by mac on 16/2/26.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD


let YSHomeViewControllerReusered = "YSHomeViewControllerReusered"

class YSHomeViewController: YSBaseTableViewController {

    var statusListViewModel: YSStatusListViewModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        if isLogin {
            
            statusListViewModel = YSStatusListViewModel()
            
           loadData()
            tableView.rowHeight = UITableViewAutomaticDimension
            tableView.estimatedRowHeight = 200
            
            tableView.registerClass(YSHomeTableViewCell.self, forCellReuseIdentifier: YSHomeViewControllerReusered)
            
            tableView.tableFooterView = pullView
            tableView.addSubview(refreshView)
            refreshView.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
        }
        
        
//
        
/*               tableview.registerClass(UITableViewCell.self,forCellReuserIdentifier:YSHomeViewControllerReusered)*/
/* tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: YSHomeViewControllerReusered)*/
    
    }

    func loadData(){
        SVProgressHUD.show()
        var since_id = statusListViewModel?.array.first?.statusModel?.id ?? 0
        var max_id = 0
        
        if pullView.isAnimating(){
            since_id = 0
            max_id = statusListViewModel?.array.last?.statusModel?.id ?? 0
            max_id -= 1
        }
        
        statusListViewModel?.loadData(since_id, max_id: max_id, finish: { (count) -> () in
            self.tableView.reloadData()
            if !self.pullView.isAnimating(){
                self.showNewCount(count)
            }
            //还原加载标记
            self.pullView.stopAnimating()
            SVProgressHUD.dismiss()
            self.refreshView.endRefreshing()
            
        })
        
    }
    
    //MARK:--显示刷新提示
    private func showNewCount(count: Int){
        //创建视图
        let showMessageLabel = UILabel (frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 44))
        //设置属性
        showMessageLabel.backgroundColor = UIColor.orangeColor()
        if count > 0 {
            showMessageLabel.text = "更新了\(count)条数据"
        }else {
            showMessageLabel.text = "暂无更新"
        }
        showMessageLabel.textAlignment = .Center
        showMessageLabel.textColor = UIColor.blackColor()
        showMessageLabel.font = UIFont.systemFontOfSize(16)
        //添加到视图里
        self.navigationController?.view.insertSubview(showMessageLabel, belowSubview: (navigationController?.navigationBar)!)
        //需要一个动画
        UIView.animateWithDuration(2, animations: { () -> Void in
            //回到原点
            showMessageLabel.transform = CGAffineTransformMakeTranslation(0, 64)
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(2, animations: { () -> Void in
                    showMessageLabel.transform = CGAffineTransformIdentity
                    }, completion: { (_) -> Void in
                        
                        showMessageLabel.removeFromSuperview()
                })
                
        }
    }
    
    
    //MARK: 懒加载
    private lazy var pullView : UIActivityIndicatorView = UIActivityIndicatorView (activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
    
    private lazy var refreshView: YSRefreshView = YSRefreshView()
    
}




//MARK:--实现数据源方法
extension YSHomeViewController{
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel?.array.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
//        let cell = tableView.dequeueReusableCellWithIdentifier(YSHomeViewControllerReusered, forIndexPath: indexPath) as! YSHomeTableViewCell
//        
//        cell.statusViewModel = statusListViewModel?.array[indexPath.row]

        let cell = tableView.dequeueReusableCellWithIdentifier(YSHomeViewControllerReusered, forIndexPath: indexPath) as! YSHomeTableViewCell
//        let vm =  statusListViewModel?.array[indexPath.row]
        cell.statusViewModel = statusListViewModel?.array[indexPath.row]

        if indexPath.row == (statusListViewModel!.array.count - 1) && (pullView.isAnimating() == false){
            //请求数据
            //先转,再加载数据
            pullView.startAnimating()
            loadData()
        }
        
        return cell
        
    }
    
    
}




